/*
 * Copyright (C), 2014-2014, 联创车盟汽车服务有限公司
 * FileName: UserController.java
 * Author:   孙昊(sunhao@lianchuang.com)
 * Date:     2014年12月10日 上午9:50:22
 * Description:
 * History:
 * <author>   <time>    <version>    <desc>
 * 修改人姓名                 修改时间                 版本号                               描述
 */
package com.linkage.category.web;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.linkage.user.interceptor.NeedLogin;
import com.linkage.user.pojo.User;
import com.linkage.user.service.UserService;

/**
 * <br>
 *
 * @author 孙昊(sunhao@lianchuang.com)
 */
@Controller
@RequestMapping("/user5")
public class UserController5 {
    @Autowired
    private UserService userService;

    @RequestMapping(value = "/list", method = RequestMethod.GET)
    @NeedLogin
    public String list(Model model) {
        List<User> users = this.userService.queryAll();
        model.addAttribute("users", users);

        return "user/list";
    }

    @RequestMapping(value = "/updateView/{userId}", method = RequestMethod.GET)
    @NeedLogin
    public String updateView(Model model, @PathVariable("userId") String userId) {
        User user = this.userService.loadOne(userId);
        model.addAttribute("user", user);

        return "user/updateView";
    }

    @RequestMapping(value = "/update", method = RequestMethod.POST)
    @NeedLogin
    public String update(User user) {
        this.userService.update(user);

        return "redirect:/user/list";
    }
    
    @RequestMapping(value = "/delete/{userId}", method = RequestMethod.GET)
    @NeedLogin
    public String delete(User user, @PathVariable("userId") String userId) {
        this.userService.delete(userId);

        return "redirect:/user/list";
    }
    
    @RequestMapping(value = "/addView", method = RequestMethod.GET)
    @NeedLogin
    public String addView() {
        return "user/addView";
    }
    
    @RequestMapping(value = "/save", method = RequestMethod.POST)
    @NeedLogin
    public String save(User user) {
        this.userService.save(user);
        
        return "redirect:/user/list";
    }
}
